#include <iostream>
#include <unistd.h>
#include <string.h>
using namespace std;

#define Size 1024
int main()
{
    int pipefd[2];
    int ret = pipe(pipefd);
    pid_t id = fork();

    if (id == 0)
    {
        close(pipefd[1]);   // 关写
        while (1)
        {
            char res[Size] = {0};
            read(pipefd[0], res, sizeof(res)); // 从管道中读出需要执行的任务
            
            cout << "Son read: " << (char *)res << endl<<endl;
        }
    }

    close(pipefd[0]); // 关读

    char res[Size] = {0};

    //cout << "Father input: ";

    while (true)
    {
        sleep(1);
        cout << "Father input: ";
        fflush(stdout);
        read(0, res, sizeof res);

        res[strlen(res) - 1] = '\0';

        write(pipefd[1], res, strlen(res));
    }
}
